﻿using System.Windows;
using System.Windows.Controls;
using LiveTiss.Schemas;

namespace LiveTiss.Guias.UserControls
{
    public partial class OPMUtilizada : UserControl
    {
        #region Value

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
            typeof(ct_OPMUtilizada), typeof(OPMUtilizada), new PropertyMetadata(null));

        public ct_OPMUtilizada Value
        {
            get { return (ct_OPMUtilizada)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        #endregion

        public OPMUtilizada()
        {
            this.DataContext = this;

            if (this.Value == null)
            {
                this.Value = new ct_OPMUtilizada()
                {
                    OPM = new System.Collections.Generic.List<ct_OPMUtilizadaIdentificacaoOPM>(),
                    valorTotalOPM = 0,
                    valorTotalOPMSpecified = true
                };
            }

            InitializeComponent();
        }

        private void btnNovo_Click(object sender, RoutedEventArgs e)
        {
            var list = this.Value.OPM.GetRange(0, this.Value.OPM.Count);

            list.Add(new ct_OPMUtilizadaIdentificacaoOPM()
            {
                codigoBarra = string.Empty,
                OPM = new ct_tabela() 
                { 
                    codigo = string.Empty,
                    descricao = string.Empty,
                    tipoTabela = st_tabela.Item00
                },
                quantidade = 0,
                valorTotal = 0,
                valorTotalSpecified = true,
                valorUnitario = 0,
                valorUnitarioSpecified = true
            });

            this.Value.OPM = list;
        }

        private void btnExcluir_Click(object sender, RoutedEventArgs e)
        {
            ContentControl ctrl = sender as ContentControl;

            var item = ctrl.DataContext as ct_OPMUtilizadaIdentificacaoOPM;

            var list = this.Value.OPM.GetRange(0, this.Value.OPM.Count);

            list.Remove(item);

            this.Value.OPM = list;
        }
    }
}
